<?php

namespace app\common\command;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\market\Acceleration;
use app\admin\model\shopro\activity\Groupon;
use app\admin\model\shopro\order\Order;
use app\admin\model\shopro\order\OrderItem;
use app\admin\model\shopro\user\WalletLog;
use app\common\model\shopro\DividendLog;
use app\common\model\shopro\ReleaseAccelerateLogModel;
use app\common\model\shopro\ReleaseAccelerateMarketplaceLogModel;
use app\common\model\shopro\Shop;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\exception\HttpResponseException;

class Shopdividend extends Command
{
    protected function configure()
    {
        $this->setName('shopdividend')->setHelp('店铺分红')->setDescription('店铺分红');
    }

    protected function execute(Input $input, Output $output)
    {
        $orderModel       = new Order();
        $dividendLogModel = new DividendLog();
        $shopModel        = new Shop();
        $info             = $dividendLogModel->where(['dividend_type' => 3])->whereTime('createtime', 'd')->find();
        if(config('site.tiaoshi') == 1){
            $info = '';
        }
        if (!$info) {
            $orderIds = $orderModel->where('shop_id','>',0)->whereIn('status', 'paid,completed')->whereTime('createtime',
                'd')->field('id,shop_id,order_amount,pay_fee,activity_id')->select();
            if($orderIds){
                $order_amount = 0;
                $virtual_amount = 0;
                $orderItemModel = new OrderItem();
                $shopArray = [];
                foreach ($orderIds as $key=>$val){
                    $normal_amount = $orderItemModel->where(['goods_type'=>'virtual','order_id'=>$val['id'],'refund_status'=>0])->count();
                    if($normal_amount){
                        $virtual_amount +=$val['pay_fee'];
                    }else{
                        $normal_amount = $orderItemModel->where(['goods_type'=>'normal','order_id'=>$val['id'],'refund_status'=>0])->value('pay_fee');
                        if($normal_amount){
                            $order_amount +=$normal_amount;
                        }
                    }
                    $shopArray[$val['shop_id']]['virtual_amount'] = $virtual_amount;
                    $shopArray[$val['shop_id']]['normal_amount']  = $order_amount;
                }
                $order_amount = bcadd($virtual_amount, $order_amount,2);
                if ($order_amount && $shopArray) {
                    $result = false;
                    $all_golden = 0;
                    Db::startTrans();
                    try {
                        $golden            = bcmul($order_amount, '0.01', 2);
                        foreach ($shopArray as $shop_id=>$v){
                            $shop_order_amount = bcadd($v['virtual_amount'], $v['normal_amount'],2);
                            $user_golden       = round($shop_order_amount / $order_amount * $golden);
                            $user_ids = '';
                            $k =  $shopModel->where(['id' => $shop_id])->value( 'user_id');
                            $result   = WalletService::change($k, 'golden', $user_golden,
                                'shopbutie_golden',
                                [],
                                '店铺分红');
                            $user_ids .= ','.$k;
                            $all_golden +=$user_golden;
                        }
                        Db::commit();
                    } catch (HttpResponseException $e) {
                        Db::rollback();
                        $data    = $e->getResponse()->getData();
                        $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
                        format_log_error($e, 'Release.release.HttpResponseException', $message);
                    }
                    if ($result) {
                        $dividendLogModel->create([
                            'user_ids'      => trim($user_ids, ','),
                            'user_num'      => count($shopArray),
                            'all_golden'    => $all_golden,
                            'level'         => 0,
                            'dividend_type' => 3
                        ]);
                        echo '分佣成功';
                    }
                }
            }
        } else {
            echo '今日已分佣';
        }
    }

}
